<?php

namespace app\common\model\mysql;

use app\common\business\DataAuth;
use think\Model;

class SPartners extends Model
{
    /**
     * 自动生成写入时间
     * @var bool
     */
    protected $autoWriteTimestamp = true;

    /**
     * 新增前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeInsert($model){
        $userData = (new DataAuth())->getUserDetails();
        $model->create_by = $userData['userName'];
    }

    /**
     * 更新前
     * @param $model
     * @return mixed|void
     */
    protected static function onBeforeUpdate($model)
    {
        $userData = (new DataAuth())->getUserDetails();
        $model->update_by = $userData['userName'];
    }

    /**
     * 查询合作商数据列表
     * @param $where
     * @param $field
     * @param $limit
     * @param $page
     * @return \think\Paginator
     * @throws \think\db\exception\DbException
     */
    public function getNormalPartnersList($where, $field,$limit,$page){
        $order = [
            "a.partners_id" =>"asc",
        ];
        $result = $this->alias('a')->join('s_partners_category b', 'a.partners_category_id = b.partners_category_id')->where($where)->field($field)->order($order)
            ->paginate([
                'list_rows' => $limit,
                'page' => $page,
            ]);
        return $result;
    }

    public function getPartnersByNameModel($partners_name,$partners_id){
        if(empty($partners_name)){
            return false;
        }
        $where = [
            "partners_name" =>$partners_name,
            "del_flag"=>'0',
        ];
        if($partners_id){
            $result = $this->where("partners_id", "<>", $partners_id)->where($where)->find();
        }else{
            $result = $this->where($where)->find();
        }
        return $result;
    }

    /**
     * 更改合作商状态
     * @param $userId
     * @param $status
     * @return bool
     */
    public function changeStatusPartnersModel($partnersId,$status){
        if(empty($partnersId)){
            return false;
        }
        $where = [
            ['partners_id', '=', $partnersId]
        ];
        $data = [
            'status'=>$status
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }

    /**
     * 根据id获取合作商信息
     * @param $id
     * @return SysDictData|array|false|mixed|Model|null
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getPartnersIdData($id){
        if(empty($id)){
            return false;
        }
        $where = [
            "partners_id" =>$id,
        ];
        $result = $this->where($where)->find();
        return $result;
    }

    public function delPartnersIds($ids){
        if(empty($ids)){
            return false;
        }
        $where = [
            ['partners_id', 'in', $ids],
            ['del_flag', '=', '0']
        ];
        $data = [
            'del_flag'=>'2'
        ];
        $result = $this->where($where)->save($data);
        return $result;
    }
}
